//
// Copyright (c) 2009 All Right Reserved
//
// vl
//
// 2009-01-01
// Contains ...
using System.Text;
namespace LargoCommon.Music
{
///
/// Musical Clef.
///
public class MusicalClef {
#region Private fields
///
/// Type Of Clef.
///
private readonly ClefType typeOfClef;
#endregion
#region Constructors
///
/// Initializes a new instance of the class.
///
/// Type of the clef.
/// The which line.
public MusicalClef(ClefType clefType, int whichLine) {
//// noteHeight = new NoteHeight("G", 0, 4);
this.typeOfClef = clefType;
this.Line = whichLine;
this.ClefPitch = ToClefMidiPitch(this.typeOfClef);
switch (this.typeOfClef)
{
case ClefType.GClef:
this.Height = new NoteHeight("G", 0, 4);
break;
case ClefType.FClef:
this.Height = new NoteHeight("F", 0, 3);
break;
case ClefType.CClef:
this.Height = new NoteHeight("C", 0, 4);
break;
}
}
#endregion
#region Properties
///
/// Gets the Note Height.
///
/// Property description.
public NoteHeight Height { get; }
///
/// Gets the type of clef.
///
/// Property description.
public ClefType TypeOfClef => this.typeOfClef;
///
/// Gets the line.
///
/// Property description.
public int Line { get; }
///
/// Gets the clef pitch.
///
/// Property description.
public int ClefPitch { get; }
#endregion
#region Public static methods
///
/// Suggests the clef for A note.
///
/// The given midi pitch.
///
/// Returns value.
///
[JetBrains.Annotations.UsedImplicitlyAttribute]
public static MusicalClef SuggestClefForMidiNote(int givenMidiPitch)
{
const int breakMidiPitch = 60; //// 55
return givenMidiPitch < breakMidiPitch ? new MusicalClef(ClefType.FClef, 4) : new MusicalClef(ClefType.GClef, 2);
}
///
/// Suggests the clef for A note.
///
/// The given midi pitch.
/// The current clef.
///
/// Returns value.
///
[JetBrains.Annotations.UsedImplicitlyAttribute]
public static MusicalClef SuggestClefForMidiNote(int givenMidiPitch, MusicalClef currentClef)
{
switch (currentClef.TypeOfClef)
{
case ClefType.GClef:
if ((currentClef.Line == 1) && (givenMidiPitch < 59)) {
return new MusicalClef(ClefType.FClef, 4);
}
if ((currentClef.Line == 2) && (givenMidiPitch < 55)) {
return new MusicalClef(ClefType.FClef, 4);
}
if ((currentClef.Line == 3) && (givenMidiPitch < 52)) {
return new MusicalClef(ClefType.FClef, 4);
}
if ((currentClef.Line == 4) && (givenMidiPitch < 48)) {
return new MusicalClef(ClefType.FClef, 4);
}
if ((currentClef.Line == 5) && (givenMidiPitch < 45)) {
return new MusicalClef(ClefType.FClef, 4);
}
return currentClef;
case ClefType.FClef:
if ((currentClef.Line == 1) && (givenMidiPitch > 74)) {
return new MusicalClef(ClefType.GClef, 2);
}
if ((currentClef.Line == 2) && (givenMidiPitch > 71)) {
return new MusicalClef(ClefType.GClef, 2);
}
if ((currentClef.Line == 3) && (givenMidiPitch > 67)) {
return new MusicalClef(ClefType.GClef, 2);
}
if ((currentClef.Line == 4) && (givenMidiPitch > 64)) {
return new MusicalClef(ClefType.GClef, 2);
}
if ((currentClef.Line == 5) && (givenMidiPitch > 60)) {
return new MusicalClef(ClefType.GClef, 2);
}
return currentClef;
default:
return new MusicalClef(ClefType.GClef, 2);
}
}
#endregion
#region String representation
/// String representation of the object.
/// Returns value.
public override string ToString()
{
var s = new StringBuilder();
s.AppendFormat("Clef {0} {1} Pitch {2}", this.TypeOfClef, this.Height, this.ClefPitch);
return s.ToString();
}
#endregion
#region Private static methods
///
/// Toes the clef midi pitch.
///
/// The type.
/// Returns value.
private static int ToClefMidiPitch(ClefType type) {
switch (type) {
case ClefType.CClef:
return 60;
case ClefType.FClef:
return 53;
case ClefType.GClef:
return 67;
default:
return 0;
}
}
#endregion
}
}